package cn.edu.hqu.cst.android.ls.viewtext;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Message;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

public class frame extends AppCompatActivity {

    private int currentColor = 0;
    protected static final String ACTIVITY_TAG="FramLayout";

    final int[] colors = new int[] {R.color.color1,
            R.color.color2,
            R.color.color3,
            R.color.color4,
            R.color.color5,
            };


    final int[] names = new int[] {R.id.view1,
            R.id.view2,
            R.id.view3,
            R.id.view4,
            R.id.view5,

    };

    ImageView[]views= new ImageView[names.length];

//创建一个匿名内部类，此类继承父类Handler并重写handleMessage方法。并立即创建此类的一个实例，创建后由handler指向此对象
    Handler handler= new Handler()
    {
        @Override
        public void handleMessage(Message msg)
            {
        if(msg.what == 0x123)
        {

                for(int i = 0; i < names.length; i++)
                {
                    views[i].setBackgroundResource(colors[(i + currentColor) % names.length]);
                }
                currentColor++;
            //Log.d(tag,message),把debug级别的日志打印到LogCat中
            Log.d(ACTIVITY_TAG,"handleMessage()"+currentColor);
        }
        super.handleMessage(msg);//这里的super就是Handler
      }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame);

        for(int i = 0; i < names.length; i++)
        {
            views[i] = (ImageView)findViewById(names[i]);
        }

        //1.创建一个匿名内部类，此类继承父类TimerTask并重写run方法。
        //2.创建一个Timer对象并调用schedule()来启动定时器
        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                handler.sendEmptyMessage(0x123);
            }
        },0,200);

    }
    }

